﻿using System;

namespace Complaint.Common
{
    public static class Utility
    {
        public static string GetRandomWord(int numberOfChars)
        {
            if (numberOfChars > 36)
                throw new InvalidOperationException("Random word characters cannot be more than 36.");

            const string strString = "abcdefghijklmnopqrstuvwxyz0123456789";

            var random = new Random();
            var captcha = "";

            for (var i = 0; i < numberOfChars; i++)
            {
                var randomCharIndex = random.Next(0, strString.Length);
                var randomChar = strString[randomCharIndex];
                captcha += Convert.ToString(randomChar);
            }

            return captcha;
        }

        public static string ToShorter(this string value, int length)
        {
            if (value.Length <= length)
                return value;

            return value.Remove(length).PadRight(length+3, '.');
        }

        public static string GetMimeType(string fileName)
        {
            var mimeType = "application/unknown";
            var ext = System.IO.Path.GetExtension(fileName).ToLower();
            var regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }

    }
}
